package com.qk.mybatis;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.qk.mybatis.pojo.Student;
import com.qk.mybatis.pojo.StudentStatus;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {
		String resource = "mybatis_conf.xml";
		InputStream is = App.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();

		String statement = "com.qk.mybatis.mapping.studentStatusMapper.getAllStudentStatus";
		List<StudentStatus> status = session.selectList(statement);
		for (StudentStatus studentStatus : status) {
			System.out.println(studentStatus);
		}

		// selectAllStudent(session);

		// Student st = selectStudentById(session, 3);
		// System.out.println();
		// st.setFirstname("2愣子");
		// updateStudent(session, st);
		// st = selectStudentById(session, 3);

		// st.setFirstname("王");
		// st.setLastname("波波");
		// st.setId(6);
		// st.setGender("男");
		// st.setSid(789);
		// insertStudent(session, st);
		// System.out.println();
		// selectAllStudent(session);

		session.close();

	}

	private static void selectAllStudent(SqlSession session) {
		String statement = "com.qk.mybatis.mapping.studentMapper.getAllStudents";
		List<Student> students = session.selectList(statement);
		for (Student student : students) {
			System.out.println(student);
		}
	}

	private static Student selectStudentById(SqlSession session, int id) {
		String statement = "com.qk.mybatis.mapping.studentMapper.getStudentById";
		Student student = session.selectOne(statement, id);
		System.out.println(student);
		return student;

	}

	private static void insertStudent(SqlSession session, Student student) {
		String statement = "com.qk.mybatis.mapping.studentMapper.insertStudent";
		session.insert(statement, student);
		session.commit();
	}

	private static void deleteStudent(SqlSession session, int id) {
		String statement = "com.qk.mybatis.mapping.studentMapper.deleteStudent";
		session.delete(statement, id);
		session.commit();
	}

	private static void updateStudent(SqlSession session, Student student) {
		String statement = "com.qk.mybatis.mapping.studentMapper.updateStudent";
		session.update(statement, student);
		session.commit();
	}
}
